// pages/readingdetailed/readingdetailed.js
import Toast from '@vant/weapp/toast/toast';
Page({


  /**
   * 页面的初始数据
   */
  data: {
    sourceUuid: '',
    chaptersData: [],
    novelContentData: [],
    showCatalogue: false,
    showBar: false,
    bookName: '',
    // 第几章
    currentIndex: 0,
    //用来做本地存储的酷匠小说id 
    bookId: '',
    // 是否收费
    isFees: false,

    // 字体弹出层
    showSizePopup: false,

    colorArr: ['#e8eaef', '#eadbbe', '#e5cecb', '#bcdcbc'],
    selecBgtColor: 0,
    // 默认字体大小
    fonsSize: 40,

    cover: '',

    // 是否已加入书架
    isBookShelf: false,
    isDark: '',
    showContent: false
  },

  changeBookShelf() {
    if (!this.data.isBookShelf) {
      this.addBookShelfFn()


    } else {
      this.delBookShelfFn()
    }

  },

  addBookShelfFn() {
    wx.cloud.callFunction({
      name: 'addBookShelf',
      data: {
        bookId: this.data.bookId,
        sourceUuid: this.data.sourceUuid,
        v_book: this.data.bookName,
        cover: this.data.cover,
        type: "bookShelf"

      }
    }).then(() => {
      // this.getBook()
      Toast.success('已加入书架');
      this.setData({
        isBookShelf: true
      })
    })
  },

  delBookShelfFn() {
    wx.cloud.callFunction({
      name: 'delBookShelf',
      data: {
        bookId: this.data.bookId,
      }
    }).then(() => {
      // this.getBook()
      Toast.success('已移出书架');
      this.setData({
        isBookShelf: false
      })
    })
  },

  getBook(id) {
    // console.log(id);
    wx.cloud.callFunction({
      name: 'getBook',
      data: {
        bookId: id,
      }
    }).then((res) => {
      // console.log('成功', res);
      if (res.result.currentList.length != 0) {
        this.setData({
          isBookShelf: true
        })
      }

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: options.bookName,
    })

    // 通过数据库查看是否加入书架
    this.getBook(options.bookId)
    // 是否是夜间模式
    let storageDark = wx.getStorageSync('storageDark') ? wx.getStorageSync('storageDark') : false
  
    this.setData({
      isDark: storageDark
    })

    // 加入历史记录
    let bookList = wx.getStorageSync('storageBookList') ? wx.getStorageSync('storageBookList') : []
    bookList = bookList.filter(t => t.bookId != options.bookId)

    bookList.unshift({
      bookId:options.bookId,
      sourceUuid: options.sourceUuid,
      v_book: options.bookName,
      cover: options.cover,
      type: "bookShelf",
      time:Date.now()
    })

    wx.setStorageSync('storageBookList', bookList)



    //  是否是点击章节跳的
    if (!options.currentIndex) {
      let currentReadChapters = wx.getStorageSync('readChapters') ? wx.getStorageSync('readChapters') : []

      currentReadChapters.map(t => {
        if (t.bookId == options.bookId) {
          this.setData({
            currentIndex: t.currentIndex
          })
        }
      })
    } else {
      console.log( options.currentIndex);
      this.setData({
        currentIndex: options.currentIndex
      })
    }



    if (JSON.parse(options.list).length != 0) {
      this.setData({
        sourceUuid: options.sourceUuid,
        bookId: options.bookId,
        chaptersData: JSON.parse(options.list),
        bookName: options.bookName,
        cover: options.cover
      })
      this.getNovelContentData()

    } else {
      this.setData({
        sourceUuid: options.sourceUuid,
        bookId: options.bookId,
        bookName: options.bookName,
        cover: options.cover
      })
      this.getBookId()

    }
    // this.testFn()
    // console.log(this.data.sourceUuid);
    // console.log(this.data.chaptersData);
  },

  // 获取小说id
  getBookId() {

    wx.request({
      url: `https://apis.netstart.cn/yunyuedu/search/book/data.json?key=${this.data.bookName}`,
      success: ({ data }) => {
        // console.log(data.data.books);
        this.setData({
          sourceUuid: data.data.books[0].sourceUuid,
        })
        this.getChaptersData()

      }
    })
  },

  // 获取小说章节
  getChaptersData() {
    wx.request({
      url: `https://apis.netstart.cn/yunyuedu/reader/book/info.json?source_uuid=${this.data.sourceUuid}`,
      success: ({ data }) => {
        this.setData({
          chaptersData: data.data.catalog,
        })
        // console.log(this.data.chaptersData);
        this.getNovelContentData()
      }
    })
  },

  // 获取当前章节内容
  getNovelContentData() {

    if (this.data.chaptersData[this.data.currentIndex].needPay) {

      this.setData({
        isFees: true
      })
      return
    }

    this.setData({
      novelContentData: [],
      showContent: false
    })


    wx.request({
      url: `https://apis.netstart.cn/yunyuedu/reader/book/content.json?source_uuid=${this.data.sourceUuid}&content_uuid=${this.data.chaptersData[this.data.currentIndex].uuid}`,
      success: ({ data }) => {
        console.log(data);
        this.setData({
          novelContentData: data.data.content,
          isFees: false,
          showContent: true
        })


        //  历史记录
        let currentReadChapters = wx.getStorageSync('readChapters') ? wx.getStorageSync('readChapters') : []

        currentReadChapters = currentReadChapters.filter(t => t.bookId != this.data.bookId)

        currentReadChapters.push({
          bookId: this.data.bookId,
          currentIndex: this.data.currentIndex
        })

        wx.setStorageSync('readChapters', currentReadChapters)


      }
    })
  },

  changeShowCatalogue() {
    this.setData({
      showCatalogue: !this.data.showCatalogue
    })
  },

  changeShowBar() {
    this.setData({
      showBar: !this.data.showBar
    })
  },

  // 上一章
  subtractCurrentIndex() {
    if (this.data.currentIndex*1 - 1 >= 0) {
      this.setData({
        currentIndex: this.data.currentIndex*1 - 1
      })
      // console.log(this.data.currentIndex);
      this.getNovelContentData()
    }

  },
  // 下一章
  addCurrentIndex() {
    if (this.data.currentIndex*1 + 1 <= this.data.chaptersData.length) {

      this.setData({
        currentIndex: this.data.currentIndex*1 + 1
      })
      // console.log(this.data.currentIndex);

      this.getNovelContentData()
    }
  },
  // 设置当前章节
  setCurrentIndex(e) {
    this.setData({
      currentIndex: e.detail,
      showCatalogue: false
    })
    this.getNovelContentData()
  },
  // 字体弹出层
  onChangeSizePopup() {
    this.setData({ showSizePopup: !this.data.showSizePopup });
  },

  changeSelecBgtColor(e) {
    this.setData({
      selecBgtColor: e.target.dataset.index
    })

  },

  changeFonsSize(e) {

    let num = this.data.fonsSize + e.target.dataset.num * 1

    if (num <= 24 || num >= 50) {
      return
    }

    this.setData({
      fonsSize: num
    })
    console.log(this.data.fonsSize);
  },

  changeIsDark() {
    console.log(this.data.isDark);
    this.setData({
      isDark: !this.data.isDark
    })

    wx.setStorageSync('storageDark', this.data.isDark)

    // if (this.data.isDark) {
    //   wx.setNavigationBarColor({
    //     backgroundColor: '#000',
    //     frontColor: '#fff',
    //   })
    // } else {
    //   wx.setNavigationBarColor({
    //     backgroundColor: '#4888ff',
    //     frontColor: '#fff',
    //   })
    // }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})